const packageJson = require("./package.json");
// 代码压缩
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
// 引入
// const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const productionGZipExtensions = ["js", "css"]; //压缩的文件类型
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const path = require("path");
function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: "./",
  productionSourceMap: false,
  outputDir: packageJson.name,
  devServer: {
    proxy: {
      "/domb": {
        target: "http://ibsaas-test-1.rongdasoft.com/",
      },
      "/rfs": {
        target: "http://192.168.7.189",
      },
      "/doc": {
        target: "http://192.168.7.189",
      },
      "/disk": {
        target: "http://192.168.7.189:20700",
      },
      "/app": {
        target: "http://192.168.7.188",
      },
      "/global": {
        target: "http://192.168.7.189",
      },
    },
  },

  // 页面均用于测试， 导出操作均在components文件夹中
  pages: {
    index: {
      entry: "src/components/index.js",
      template: "public/index.html",
      filename: "index.html",
      title: "荣大智慧云",
    },
    loginWeb: {
      entry: "src/login-web/main.js",
      template: "public/index.html",
      filename: "index.web.html",
      title: "网页智慧云",
    },
    loginMobile: {
      entry: "src/login-mobile/main.js",
      template: "public/index.html",
      filename: "index.mobile.html",
      title: "手机智慧云",
    },
  },
  // 展示图形化信息
  chainWebpack: (config) => {
    // config.plugin("webpack-bundle-analyzer").use(BundleAnalyzerPlugin);
    // 移除prefetch插件，避免加载多余的资源
    config.plugins.delete("prefetch");
    // 移除 preload 插件，避免加载多余的资源
    config.plugins.delete("preload");
  },
  configureWebpack: (config) => {
    // 开启gzip压缩
    config.plugins.push(
      new CompressionWebpackPlugin({
        filename: (info) => {
          return `${info.path}.gz${info.query}`;
        },
        algorithm: "gzip",
        threshold: 10240, // 只有大小大于该值的资源会被处理 10K
        test: new RegExp("\\.(" + productionGZipExtensions.join("|") + ")$"),
        minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
        deleteOriginalAssets: false, // 删除原文件
      })
    );
    config.plugins.push(
      // 代码压缩
      new UglifyJsPlugin({
        uglifyOptions: {
          //生产环境自动删除console
          compress: {
            drop_debugger: true,
            drop_console: true,
            pure_funcs: ["console.log"],
          },
        },
        sourceMap: false,
        parallel: true,
      })
    ),
      // webpack打包体积的性能提示
      (config["performance"] = {
        maxEntrypointSize: 50000000,
        maxAssetSize: 30000000,
      });
    config.resolve.alias["@"] = resolve("src");
    config.resolve.alias["components"] = resolve("src/components");
    config.resolve.alias["api"] = resolve("src/components/api");
    config.resolve.alias["utils"] = resolve("src/components/utils");
    config.resolve.alias["plugins"] = resolve("src/components/plugins");
  },
};
